// index.js
// 获取应用实例
import request from "../../utils/request"
const app = getApp()
let startY = 0; // 手指起始的坐标
let moveY = 0; // 手指移动的坐标
let moveDistance = 0; // 手指移动的距离
Page({
  data: {
    coverTransform: 'translateY(0)',
    coveTransition: '',
    userInfo:{},
    recentPlayList:[],
  },
 
  onLoad() {
    
  },
  onReady(){
    this.getUserInfo()
    this.getHistoryInfo()
  },
  handleTouchStart(event){
    this.setData({
      coveTransition: ''
    })
    // 获取手指起始坐标
    startY = event.touches[0].clientY;
  },
  handleTouchMove(event){
    moveY = event.touches[0].clientY;
    moveDistance = moveY - startY;
    
    if(moveDistance <= 0){
      return;
    }
    if(moveDistance >= 80){
      moveDistance = 80;
    }
    // 动态更新coverTransform的状态值
    this.setData({
      coverTransform: `translateY(${moveDistance}rpx)`
    })
  },
  handleTouchEnd(){
    // 动态更新coverTransform的状态值
    this.setData({
      coverTransform: `translateY(0rpx)`,
      coveTransition: 'transform 1s linear'
    })
  },
  toLogin(){
    wx.redirectTo({
      url: '/pages/login/login',
    })
  },
  // 获取本地个人信息
  getUserInfo(){
    const result= JSON.parse(wx.getStorageSync("userInfo")) 
    let userInfo={}
    userInfo.avatarUrl=result.profile.avatarUrl
    userInfo.nickname=result.profile.nickname
    userInfo.id=result.account.id
    this.setData({
      userInfo,
    })
  },
  // 获取用户播放记录
  async getHistoryInfo(){
    const {userInfo}=this.data
    if(!userInfo.id) return
    const result=await request("/user/record",{uid:userInfo.id,type:1,})
    this.setData({
      recentPlayList:result.weekData,
    })
    
  }
})

